﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Donations
{
    public static class Buckets
    {
        public static Adams.Admin.DataTransfer.Donations.Buckets.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.Donations.Buckets.PageLoad();
            
            return data;
        }

        public static Adams.Admin.DataTransfer.Donations.Buckets.GridLoad GridLoad(DataTransfer.GridLoadInput gridinput)
        {
            var data = new Adams.Admin.DataTransfer.Donations.Buckets.GridLoad();
            data.Records = new Shared.Entities.Donations.BucketCollection();

            string filter = string.IsNullOrWhiteSpace(gridinput.Search) ? string.Empty : "Name LIKE '%" + gridinput.Search.Replace("'", "''") + "%'";

            string dbq = DB.BuildPagingQuery(gridinput.StartRow, gridinput.PageSize,
                "*",
                "Donations.Buckets",
                filter, string.Empty, gridinput.Sort + " " + gridinput.SortDirection, "BucketID");

            using (SqlDataReader dr = DB.RunDataReader(dbq, CommandType.Text))
            {
                dr.Read();
                data.TotalRecords = dr[0].DbInt();

                dr.NextResult();
                while (dr.Read())
                {
                    var bucket = new Shared.Entities.Donations.Bucket();
                    bucket.ID = dr["BucketID"].DbInt();
                    bucket.Name = dr["Name"].DbString();
                    bucket.IsDefault = dr["IsDefault"].DbBool();
                    bucket.AllowRecurring = dr["AllowRecurring"].DbBool();
                    data.Records.Add(bucket);
                }
            }

            return data;
        }
     }
}
